<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
 * Created on Aug 20, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
-->
	<head>
		<title>Walter J Corbett Intivational</title>
		<link href="assets/styles/liquid.css" rel="stylesheet" type="text/css" />
		<link href="assets/styles/wjci.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="assets/js/jquery-1.6.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#updatePWForm").submit(function(){
					var currPW = $("#current_password").val();
					var newPW = $("#new_password").val();
					var confPW = $("#confirm_password").val();
					
					if(!(currPW != "" && newPW != "" && confPW != "")){
						alert("All textbox must be filled.");
						return false;
					}
					else if(newPW != confPW){
						alert("new password and confirm password don't match.\nPlease try again.");
						return false;
					}
					else if(newPW == currPW){
						alert("current password and new password cannot be same.\nPlease try again.");
						return false;
					}
					else if(!(currPW.length > 4 && newPW.length > 4 && confPW.length > 4)){
						alert("Please enter at least 5 characters.");
						return false;
					}
					else{
						return true;
					}
				});
				
			});
		</script>
	</head>
<?

if(!defined("APP_ROOT")){
	$httpHost = strtolower($_SERVER["HTTP_HOST"]);
	$virDir = $_SERVER['DOCUMENT_ROOT'];
	$virDir .= ($httpHost=="localhost")?"/wjci":"";
	define("APP_ROOT",$virDir);
}

if(!function_exists("modPath")){
	function modPath($path){
		if(strtolower(PHP_OS)=="winnt"){
			$path = str_replace("/","\\",$path);	
		}
		return $path;
	}	
}
require_once(modPath(APP_ROOT."/model/Player.php"));
require_once(modPath(APP_ROOT."/bizlayer/player_mgmt.php"));
session_start();


$wrongCurrPW = false;
$pwUpdate = false;
if(isset($_POST) && $_SERVER["REQUEST_METHOD"] == "POST"){
	$currPW = $_POST["current_password"];
	$newPW = $_POST["new_password"];
	$me = (isset($_SESSION["me"]))?$_SESSION["me"]:null;
	printf("new password: %s<br/>%s<br/>%s",$newPW,$me->emailAddress,md5($newPW));
	$player = PlayerMgmt::login($me->emailAddress,md5($currPW));
	
	$wrongCurrPW = ($player == null)?true:false;
	printf("wrongCurrPW: %s<br/>",$wrongCurrPW);
	if(!$wrongCurrPW){
		PlayerMgmt::updatePassword($player->playerid,md5($newPW),1);
		$pwUpdate = true;
	}
	
	
}
?>

<body>
<div id="maincontainer">
	<form name="indexform" method="post" action="index.php">
	<?php
	require_once(modPath(APP_ROOT."/components/header.inc.php"));
	?>
	<form id="updatePWForm" name="updatePWForm" action="update_password.php" method="post">
	<div id="contentwrapper">
		<div id="contentcolumn">
			<div class="innertube">
				<p>Please enter minimum 5 chracters, maximum 10 characters and different from current password.</p>
				<table style="margin-left:25%;margin-right:25%;margin-top:25%;margin-bottom:25%">
					<tr>
						<td align="right" valign="top">Current password:</td>
						<td valign="top">
							<input type="password" id="current_password" name="current_password"/><br/>
							<?if($wrongCurrPW){?>
							<strong style="color:red">wrong input. Please try again.</strong>
							<?}?>	
						</td>
					</tr>
					<tr>
						<td align="right" valign="top">New password:</td>
						<td valign="top"><input type="password" id="new_password" name="new_password"/></td>
					</tr>
					<tr>
						<td align="right" valign="top">Confirm password:</td>
						<td valign="top"><input type="password" id="confirm_password" name="confirm_password"/></td>
					</tr>
					<tr>
						<td colspan="2" align="right">
							<input type="submit" name="updatePassword" value="Update"/>	
						</td>
					</tr>
				</table>
			</div>
		</div>
	</div>
	</form>
		<div class="innertube" style="margin-bottom:70%">
			
		</div>
	</div>
	<?php
	require_once("components/footer.inc.php");
	?>
	</form>
</div> 
</body>
</html>
